package hu.uszeged.inf.stud.prf.todo.entity.task;

import hu.uszeged.inf.stud.prf.todo.entity.user.User;
import hu.uszeged.inf.stud.prf.todo.entity.usertask.UserTask;

import java.io.Serializable;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

@Entity
@Table(name = "task_type",
	uniqueConstraints = @UniqueConstraint(columnNames={"name", "user_id", "archive"} ) )
public class TaskType implements Serializable {
	
	private static final long serialVersionUID = -5635645173372982375L;

	@Id
	@SequenceGenerator(name = "idGenSeqTaskType", initialValue=7)
	@GeneratedValue(strategy = GenerationType.TABLE, generator = "idGenSeqTaskType")
	private Long id;

	private Boolean archive;
	
	@ManyToMany(cascade = CascadeType.ALL,
			mappedBy = "tasktypes")
	private List<UserTask> usertasks;
	
	private String name;
	
	@ManyToOne
	private User user;

	public TaskType() {
		super();
		this.archive = false;
	}
	
	public TaskType(Long id, Boolean archive, List<UserTask> usertasks,
			String name) {
		super();
		this.id = id;
		this.archive = archive;
		this.usertasks = usertasks;
		this.name = name;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List<UserTask> getUsertasks() {
		return usertasks;
	}

	public void setUsertasks(List<UserTask> usertasks) {
		this.usertasks = usertasks;
	}

	public Boolean isArchive() {
		return archive;
	}

	public void setArchive(Boolean archive) {
		this.archive = archive;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((archive == null) ? 0 : archive.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result
				+ ((usertasks == null) ? 0 : usertasks.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		TaskType other = (TaskType) obj;
		if (archive == null) {
			if (other.archive != null)
				return false;
		} else if (!archive.equals(other.archive))
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (usertasks == null) {
			if (other.usertasks != null)
				return false;
		} else if (!usertasks.equals(other.usertasks))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return name;
	}
	
}
